#!/bin/python
import numpy as np
from scipy import interpolate
fname = ["mmc1.txt", "mmc2.txt", "mmc3.txt", "mmc4.txt", "mmc5.txt"]
m1 = np.loadtxt(fname[0])
m2 = np.loadtxt(fname[1])
m3 = np.loadtxt(fname[2])
m4 = np.loadtxt(fname[3])
m5 = np.loadtxt(fname[4])
lgkappa_final = m5[:, 0]
lgeta_pre = m3[0, :]
lgkappa_pre = m4[0, :]

'''
tck = interpolate.splrep(m2[:, 0], m2[:, 1], s = 0)
heta = interpolate.splev(lgeta_final, tck, der = 0)
np.savetxt("heta.txt", heta)
np.savetxt("eta.txt", lgeta_final)
np.savetxt("kappa.txt", lgkappa_final)
np.savetxt("tpm.txt", m5[:, 1])
'''
pkappa_pre = m3[1:, :]
f_pre = m3[1, :]
pfkappa_pre = m4[2:, :]


et, kp = np.mgrid[lgeta_pre[0]:lgeta_pre[-1]:100j, lgkappa_pre[0]:lgkappa_pre[-1]:100j]
print "et kp mesh over"
ntck = interpolate.bisplrep(et, kp, pkappa_pre, s = 0)
print "ntck finish"
le, lk = np.mgrid[lgkappa_pre[0]:lgkappa_pre[-1]:1101j, lgeta_pre[0]:lgeta_pre[-1]:1101j]
print "le, lk mesh over"
print et.shape, kp.shape, le.shape, lk.shape

pkappa_final = interpolate.bisplev(le[:, 0], lk[0, :], ntck, der = 0)
np.savetxt("pek.txt", pkappa_final)



kp, fp = np.meshgrid(lgkappa_pre, f_pre)
tck = interpolate.bisplrep(kp, fp, pfkappa_pre, s = 0)
lk, lf = np.meshgrid(lgkappa_final, f_final)

print kp.shape, fp.shape, lk.shape, lf.shape

pfkappa_final = interpolate.bisplev(lk[0, :], lf[:, 0], tck, der = 0)
np.savetxt("f.txt", f_final)
np.savetxt("pfk.txt", pfkappa_final)
print "generate data finished"
